package by.msokoloff.javaweb2015.webapp.page.plane;

import java.util.Iterator;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.request.resource.ContextRelativeResource;
import org.apache.wicket.request.resource.IResource;

import by.msokoloff.javaweb2015.datamodel.Plane;
import by.msokoloff.javaweb2015.datamodel.Plane_;
import by.msokoloff.javaweb2015.services.PlaneService;
import by.msokoloff.javaweb2015.webapp.page.EntityEditPage;

public class PlaneListPanel extends Panel {

	@Inject
	private PlaneService planeService;

	public PlaneListPanel(String id) {
		super(id);

		PlaneDataProvider provider = new PlaneDataProvider();

		DataView<Plane> dv = new DataView<Plane>("list", provider, 25) {

			IResource editIconResource = new ContextRelativeResource("img/editIcon.png");
			IResource deleteIconResource = new ContextRelativeResource("img/deleteIcon.png");

			@Override
			protected void populateItem(Item<Plane> item) {
				Plane plane = item.getModelObject();
				item.add(new Label("id"));
				item.add(new Label("planeModel"));
				item.add(new Label("boardNumber"));
				item.add(new Label("status"));

				Link<Void> editLink = new Link<Void>("edit") {

					@Override
					public void onClick() {
						setResponsePage(new EntityEditPage(plane));

					}

				};

				editLink.add(new Image("editicon", editIconResource));
				item.add(editLink);

				Link<Void> deleteLink = new Link<Void>("delete") {

					@Override
					public void onClick() {
						// TODO Auto-generated method stub

					}

				};

				deleteLink.add(new Image("deleteicon", deleteIconResource));
				item.add(deleteLink);
			}

		};

		WebMarkupContainer tableBody = new WebMarkupContainer("wrapper-body");
		add(tableBody);
		tableBody.add(dv);

		add(new PagingNavigator("paging", dv));

		// TODO sorting add(new OrderByBorder<SingularAttribute<Plane,
		// ?>>("sortByName", Plane_.name, provider));

		add(new Link<Void>("create") {

			@Override
			public void onClick() {
				setResponsePage(new EntityEditPage(new Plane()));

			}

		});

	}

	private class PlaneDataProvider extends SortableDataProvider<Plane, SingularAttribute<Plane, ?>> {

		public PlaneDataProvider() {
			super();
			setSort(Plane_.boardNumber, SortOrder.ASCENDING);
		}

		@Override
		public Iterator<? extends Plane> iterator(long first, long count) {
			SingularAttribute<Plane, ?> sortParam = getSort().getProperty();
			SortOrder propertySortOrder = getSortState().getPropertySortOrder(sortParam);
			boolean ascending = SortOrder.ASCENDING.equals(propertySortOrder);
			return planeService.getAll(sortParam, ascending, (int) first, (int) count).iterator();
		}

		@Override
		public long size() {
			return planeService.getCount();
		}

		@Override
		public IModel<Plane> model(Plane plane) {
			return new CompoundPropertyModel<Plane>(plane);
		}

	}

}
